package club.kingyin.kyblog.core.business.service;

import club.kingyin.kyblog.core.business.dao.Comment;
import club.kingyin.kyblog.core.business.dao.Doc;
import club.kingyin.kyblog.core.business.dao.Query;
import club.kingyin.kyblog.core.business.mapper.CommentMapper;
import club.kingyin.kyblog.core.business.mapper.DocMapper;
import club.kingyin.kyblog.core.business.mapper.TypeMapper;
import club.kingyin.kyblog.core.business.service.cloud.Image;
import club.kingyin.kyblog.core.business.service.cloud.QiniuService;
import club.kingyin.kyblog.core.business.vo.PageVo;
import club.kingyin.kyblog.core.business.vo.admin.TypeCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Random;
import java.util.Set;

/**
 * @author : kingyins
 * @version : 1.0
 * @program : ky-blog
 * @description : 在此处添加类描述
 * @create : 2021年08月24日 10:19
 **/
@Service

public class DocService {

    @Autowired
    private DocMapper docMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private QiniuService qiniuService;

    @Autowired
    private TypeMapper typeMapper;

    public int updateAuthor(String author) {
        return docMapper.updateAuthor(author);
    }


    public Set<TypeCount> getPieChart() {
        try {
            Set<TypeCount> typeCounts = docMapper.typeCount();
            return typeCounts;
        } catch (Exception e) {
            return null;
        }

    }

    public Integer[] getPanelGroup() {
        try {
            return new Integer[]{
                    docMapper.viewCount(),
                    docMapper.commentCount(),
                    docMapper.disCommentCount(),
                    docMapper.docCount()
            };
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Set<Doc> getBarChart() {
        try {
            return docMapper.hotDocLimit();
        } catch (Exception e) {
            return null;
        }
    }

    public boolean marginViewCount(int id, int viewCount) {
        return docMapper.addViewCountById(id, viewCount) > 0;
    }

    public Doc getDocById(int id) {
        return docMapper.selectById(id);
    }

    public boolean deleteById(int id) {
        commentMapper.deleteByDocId(id);
        return docMapper.deleteById(id) > 0;
    }

    public PageVo webSearchList(String context, int page, int size, boolean isType) {
        try {
            page = (page - 1) * size;
            int total;
            Set<Doc> docs;
            if (isType) {
                if (context == null) {
                    // 全部
                    total = docMapper.selectPublishedCount();
                    docs = docMapper.selectPublishedDocs(page, size);
                } else {
                    // 标签
                    total = docMapper.selectCountByType(context);
                    docs = docMapper.selectDocsByType(context, page, size);
                }
            } else {
                // 索引
                total = docMapper.selectCount(context);
                docs = docMapper.selectDocsByTitleOrSummaryOrType(context, page, size);
            }
            PageVo pageVo = new PageVo();
            pageVo.setHasNextPage(total / size > page);
            pageVo.setTotal(total);
            pageVo.setList(docs);
            pageVo.setPage(page);
            return pageVo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public PageVo adminSearchList(Query query) {
        try {
            query.setPage((query.getPage() - 1) * query.getSize());
            int total = docMapper.selectCountByAdmin(query);
            Set<Doc> docs = docMapper.selectDocsAsAdmin(query);
            PageVo pageVo = new PageVo();
            pageVo.setTotal(total);
            pageVo.setPage(query.getPage());
            pageVo.setList(docs);
            return pageVo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean createDoc(Doc doc) {
        return docMapper.insert(doc) > 0;
    }

    public boolean updateDoc(Doc doc) {
        return docMapper.updateById(doc) > 0;
    }

    @Transactional
    public boolean createComment(Comment comment) {
        Doc doc = docMapper.selectById(comment.getDocId());
        if (doc.getComment() < 0) {
            return false;
        }
        // 从七牛云随机选择一张头像
        ArrayList<String> avatars = qiniuService.getUrlsByPrefix(Image.AVATAR);
        comment.setFromUserAvatar(avatars.get(new Random().nextInt(avatars.size())));
        boolean b = commentMapper.insert(comment) > 0;
        // 评论+1
        doc.setDisComment(doc.getDisComment() + 1);
        docMapper.updateById(doc);
        return b;
    }

    @Transactional
    public boolean publishedComment(int id) {
        boolean b = commentMapper.updateStatusById(id, 1) > 0;
        int docIdById = commentMapper.selectDocIdById(id);
        Doc doc = docMapper.selectById(docIdById);
        doc.setDisComment(doc.getDisComment() - 1);
        doc.setComment(doc.getComment() + 1);
        docMapper.updateById(doc);
        return b;
    }

    public boolean delComment(int id) {
        int docIdById = commentMapper.selectDocIdById(id);
        Doc doc = docMapper.selectById(docIdById);
        if (commentMapper.selectStatusById(id) == 0) {
            // 更新doc
            doc.setDisComment(doc.getDisComment() - 1);
        } else {
            doc.setComment(doc.getComment() - 1);
        }
        docMapper.updateById(doc);
        return commentMapper.deleteById(id) > 0;
    }

    public ArrayList<Comment> getCommentsByDocId(int id) {
        if (docMapper.selectCommentById(id) <= 0) {
            return null;
        }
        return commentMapper.selectByDocId(id);
    }

    public ArrayList<Comment> getCommentContextByDocId(int id) {
        return commentMapper.selectContentByDOcId(id);
    }

}
